package com.mexico.inloancash.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.mingle.widget.LoadingView;
import com.mexico.inloancash.R;
import com.mexico.inloancash.adapter.IndiaBindBankAdapter;
import com.mexico.inloancash.bean.India_BankingCheck_ListBean;
import com.mexico.inloancash.http.IndiaRequestApi;
import com.mexico.inloancash.http.IndiaDKFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 我的银行卡
 */
public class India_Bank_Card_Activity extends BaseActivity {

    @BindView(R.id.logon_back)
    ImageView mIndia_GonBack;

    @BindView(R.id.title)
    TextView title;

    @BindView(R.id.recycle_view)
    RecyclerView mIndia_RecycleView;

    @BindView(R.id.add_bank)
    LinearLayout mIndia_AddBank;

    @BindView(R.id.loadView)
    LoadingView mIndiaLoadingView;

    @BindView(R.id.nestedScrollView)
    NestedScrollView mIndiaScrollView;

    private List<India_BankingCheck_ListBean.DataBean.BankListsBean> mIndiaBankListsBeans;
    private IndiaBindBankAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.india_activity_view_bank);
        ButterKnife.bind(this);
        title.setText("Mi tarjeta bancaria");
        mIndiaBankListsBeans = new ArrayList<>();
        adapter = new IndiaBindBankAdapter();
        mIndia_RecycleView.setAdapter(adapter);
        India_GetBankList();
    }

    public void India_GetBankList() {
        Log.d("loan", "getBankList");
        IndiaRequestApi api = IndiaDKFactory.getInstance().getApi();
        Call<String> getBank = api.getBankList();
        getBank.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                try {
                    Log.d("loan", "getBankList onResponse:" + response.body());
                    if (response.isSuccessful()) {
                        India_BankingCheck_ListBean listBean = new Gson().fromJson(response.body(), India_BankingCheck_ListBean.class);
                        if (listBean.getStatus() == 1) {
                            mIndiaBankListsBeans.clear();
                            if (null != listBean.getData().getBankLists() && listBean.getData().getBankLists().size() > 0) {
                                for (int i = 0; i < listBean.getData().getBankLists().size(); i++) {
                                    Random rd = new Random();
                                    int k = rd.nextInt(2);
                                    listBean.getData().getBankLists().get(i).setRadom(k);
                                    mIndiaBankListsBeans.add(listBean.getData().getBankLists().get(i));
                                }
                                adapter.setNewData(mIndiaBankListsBeans);
                            }
                        }
                    }
                } catch (Exception e) {

                }
                mIndiaScrollView.setVisibility(View.VISIBLE);
                mIndiaLoadingView.setVisibility(View.GONE);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                mIndiaScrollView.setVisibility(View.VISIBLE);
                mIndiaLoadingView.setVisibility(View.GONE);
            }
        });
    }

    @OnClick({R.id.logon_back, R.id.add_bank})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.logon_back:
                finish();
                break;
            case R.id.add_bank:
                Intent add = new Intent(this, IndiaAddBankActivity.class);
                startActivityForResult(add, 0);
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            India_GetBankList();
        }
    }
}
